﻿/*********************************************************
新增日期:2011.4.13
创建作者:wuweijun

目标文件：WhoYao.Common.TypeValidator.cs
功能描述：WhoYao功能初始类
*********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
{
    /// <summary>
    /// 类型校验类
    /// </summary>
    public class TypeValidator
    {
        /// <summary>
        /// 判断对象是否为Int32类型的数字
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(object expression)
        {
            if (expression != null)
                return IsNumeric(expression.ToString());

            return false;

        }

        /// <summary>
        /// 判断对象是否为Int32类型的数字
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(string expression)
        {
            if (expression != null)
            {
                string str = expression;
                if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
                {
                    if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 是否为Double类型
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool IsDouble(object expression)
        {
            if (expression != null)
                return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");

            return false;
        }

        /// <summary>
        /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
        /// </summary>
        /// <param name="strNumber">要确认的字符串数组</param>
        /// <returns>是则返加true 不是则返回 false</returns>
        public static bool IsNumericArray(string[] strNumber)
        {
            if (strNumber == null)
                return false;

            if (strNumber.Length < 1)
                return false;

            foreach (string id in strNumber)
            {
                if (!IsNumeric(id))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// 是否正确的HTTP网址
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool IsHttpSite(string url)
        {
            if (string.IsNullOrEmpty(url)) return false;
            string pattern = @"http://[0-9a-z][\w-]*((\.[\w-]+)+?){2,4}(:\d+)?";
            Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
            return r.IsMatch(url);
        }
        /// <summary>
        /// 是否正确的域名格式
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool IsDomain(string url)
        {
            if (string.IsNullOrEmpty(url)) return false;
            string pattern = @"[0-9a-z][\w-]*((\.[\w-]+)+?){2,4}";
            Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
            return r.IsMatch(url);
        }

        /// <summary>
        /// 严格判断是否IP V4
        /// </summary>
        /// <param name="str">IP String</param>
        /// <returns>Boolean</returns>
        public static bool isIPv4(string str)
        {
            /*string rule = @"\d+\.\d+\.\d+\.\d{1,3}?";*/
            bool b = false;
            if (str != null && str.Length > 6)
            {
                string[] s4 = str.Split('.');
                if (s4.Length == 4)
                {
                    for (int i = 0; i < s4.Length; i++)
                    {
                        if (!isDigit(s4[i]) || (Convert.ToInt32(s4[i]) < 0 || Convert.ToInt32(s4[i]) > 255))
                        {
                            b = false;
                            break;
                        }
                        else
                        {
                            b = true;
                        }
                    }
                }
            }
            return b;
        }

        /// <summary>
        /// 判断字符串是否全为十进制数字（首位可以是0，拒绝正负号及小数）
        /// </summary>
        /// <param name="str">String</param>
        /// <returns>Boolean</returns>
        public static bool isDigit(string str)
        {
            if (str == null || str.Length == 0) return false;
            foreach (char c in str)
            {
                if (!Char.IsDigit(c)) return false;
            }
            return true;
        }
        /// <summary>
        /// 利用尝试转换的方法判断是否int
        /// </summary>
        /// <param name="str">传入的字符串</param>
        /// <returns></returns>
        public static bool isInt32(string str)
        {
            try
            {
                if (str != null)
                {
                    System.Convert.ToInt32(str);
                    return true;
                }
            }
            catch { }
            return false;
        }
        /// <summary>
        /// 是否能将字符串转换为长整型
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static bool isInt64(string str)
        {
            try
            {
                if (str != null)
                {
                    System.Convert.ToInt64(str);
                    return true;
                }
            }
            catch { }
            return false;
        }
        /// <summary>
        /// 是否能将字符串转换为BYTE字节
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static bool isByte(string str)
        {
            try
            {
                if (str != null)
                {
                    Convert.ToByte(str);
                    return true;
                }
            }
            catch { }
            return false;
        }
        /// <summary>
        /// 是否能将字符串转换为Char字符
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static bool isChar(string str)
        {
            try
            {
                if (str != null)
                {
                    Convert.ToChar(str);
                    return true;
                }
            }
            catch { }
            return false;
        }
        /// <summary>
        /// 是否能将字符串转换为时间
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static bool isDateTime(string str)
        {
            try
            {
                if (str != null)
                {
                    Convert.ToDateTime(str);
                    return true;
                }
            }
            catch { }
            return false;
        }
        /// <summary>
        /// 是否能将字符串转换为小数
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static bool isDecimal(string str)
        {
            try
            {
                if (str != null)
                {
                    Decimal.Parse(str);
                    return true;
                }
            }
            catch { }
            return false;
        }
        /// <summary>
        /// 是否能将整型转换为二进制
        /// </summary>
        /// <param name="num">整型值</param>
        /// <returns></returns>
        public static bool isInt_Hex(int num)
        {
            try
            {
                Convert.ToString(num, 16);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 是否能将长整型转换为二进制
        /// </summary>
        /// <param name="num">长整型值</param>
        /// <returns></returns>
        public static bool isInt_Hex(long num)
        {
            try
            {
                Convert.ToString(num, 16);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 是否能将字符串转换为二进制
        /// </summary>
        /// <param name="str">字符串值</param>
        /// <returns></returns>
        public static bool isInt_Hex(string str)
        {
            try
            {
                Convert.ToInt64(str, 16);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
